package com.ysalliance.getFan.organization.ui;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.example.zhouwei.library.CustomPopWindow;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ysalliance.getFan.organization.Bean.ClassBean;
import com.ysalliance.getFan.organization.Bean.ClassSituationBean;
import com.ysalliance.getFan.organization.Bean.TaskBean;
import com.ysalliance.getFan.organization.Bean.bus.ChangeClassNameBean;
import com.ysalliance.getFan.organization.Bean.bus.DeleteClassBean;
import com.ysalliance.getFan.organization.Bean.multilist.FirstTimeBean;
import com.ysalliance.getFan.organization.LucasUtil;
import com.ysalliance.getFan.organization.R;
import com.ysalliance.getFan.organization.myview.ItemClassView;
import com.ysalliance.getFan.organization.myview.dialog.ChooseDialog;
import com.ysalliance.getFan.organization.network.Network;
import com.ysalliance.getFan.organization.ui.base.LucasBaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.List;

import androidx.annotation.NonNull;
import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

import static com.ysalliance.getFan.organization.FanerConstant.CHANGE_CLASS_NAME;


/**
 * 班级管理
 * @author lucas
 * created at 2020-02-24 14:54
 */
public class ClassManagerActivity extends LucasBaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.itemClassView_class_name)
    ItemClassView itemClassView_class_name;   //班级名称

    @BindView(R.id.itemClassView_class_share_code)
    ItemClassView itemClassView_class_share_code;   //班级分享码

    @BindView(R.id.itemClassView_class_stu_manager)
    ItemClassView itemClassView_class_stu_manager;   //学员管理

    @BindView(R.id.item_guanliyuan)
    ItemClassView item_guanliyuan;     //班级管理员

    @BindView(R.id.re_delete_class)
    View re_delete_class;  //删除班级


    public ClassBean classBean ;



    @Override
    public int bindContentView() {
        return R.layout.activity_class_manager;
    }


    @Override
    public void beforeView() {
        super.beforeView();
        EventBus.getDefault().register(mContext);
    }


    @Override
    public void afterView() {
        super.afterView();

        classBean = (ClassBean)getIntent().getSerializableExtra("class_info");

        tv_title.setText(classBean.getName());
        itemClassView_class_name.setRightText(classBean.getName());
        itemClassView_class_share_code.setRightText(classBean.getCode());

        initClick();

    }


    public void initClick() {

        // 班级管理 or 添加任务
        item_guanliyuan.setOnClickListener(v->{
            ActivityUtils.startActivity(AdminActivity.class);
        });

        //删除班级
        re_delete_class.setOnClickListener(v->{
            ChooseDialog chooseDialog = new ChooseDialog(mContext,"确认删除", "不删了", "确定要删除班级？"){
                @Override
                public void left() {
                    super.left();
                    delete_class(classBean.getId());
                }

                @Override
                public void right() {
                    super.right();
                }
            };
            chooseDialog.show();

        });


        //班级名称
        itemClassView_class_name.setOnClickListener(v->{
            Intent intent = new Intent(Utils.getApp(), EditTextOneActivity.class);
            intent.putExtra("class_info",classBean);
            intent.putExtra("class",CHANGE_CLASS_NAME);
            ActivityUtils.startActivity(intent);
        });

        //分享班级码
        itemClassView_class_share_code.setOnClickListener(v->{
            Intent intent = new Intent(Utils.getApp(), EnterCreateClassSuccessActivity.class);
            intent.putExtra("class_info",classBean);
            ActivityUtils.startActivity(intent);
        });

        //学员管理
        itemClassView_class_stu_manager.setOnClickListener(v->{
            Intent intent = new Intent(Utils.getApp(), StudentManagerActivity.class);
            intent.putExtra("class_info",classBean);
            ActivityUtils.startActivity(intent);
        });

        //管理员
        item_guanliyuan.setOnClickListener(v->{
            Intent intent = new Intent(Utils.getApp(),  AdminMannagerActivity.class);
            intent.putExtra("class_info",classBean);
            ActivityUtils.startActivity(intent);
        });



    }


    @Override
    protected void onResume() {
        super.onResume();

        getClassInfo(classBean.getId());
    }

    /**  获得班级详情的返回   */
    public void getClassInfo(String class_id){
        disposable = Network.getUtilApi()
                .getClassInfo(class_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json("获取班级详情的返回", res);

                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){
                            classBean = new Gson().fromJson(jsonObject.optString("data"), ClassBean.class);

                            freshUI(classBean);

                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }


    /**  删除班级的返回   */
    public void delete_class(String class_id){
        disposable = Network.getUtilApi()
                .delete_class(class_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json("删除班级的返回", res);

                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){
                            ToastUtils.showShort("删除成功");
                            EventBus.getDefault().post(new DeleteClassBean(true));
                            onBackPressed();
                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }


    private void freshUI(ClassBean classBean) {
        tv_title.setText(classBean.getName());
        itemClassView_class_name.setRightText(classBean.getName());
        itemClassView_class_share_code.setRightText(classBean.getCode());
        itemClassView_class_stu_manager.setRightText(classBean.getMembers()+"");

    }




    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ChangeClassNameBean changeClassNameBean) {
        /* Do something */
        LogUtils.e("收到了eventbus的更新班级名称的提示");
        /* Do something */
        classBean.setName(changeClassNameBean.getClass_name());
        freshUI(classBean);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
